﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LevenBlog.Core.Data;
using LevenBlog.Core.Entities;
using LevenBlog.Core.Utils;

namespace LevenBlog.Core.Service
{
    public static class Tag
    {
        public static int Count(IDbProvider provider)
        {
            return ProviderManager.Factory.Tag.Count(provider);
        }

        public static lb_tag Get(IDbProvider provder, int id)
        {
            return ProviderManager.Factory.Tag.Get(provder, id);
        }

        public static lb_tag Get(IDbProvider provider, string tagName)
        {
            return ProviderManager.Factory.Tag.Get(provider, Text.SqlEncode(tagName));
        }

        public static IList<lb_tag> GetOrderByHot(IDbProvider provider, int pageSize, int currentPage)
        {
            return ProviderManager.Factory.Tag.GetOrderByHot(provider, pageSize, currentPage);
        }

        public static IList<lb_tag> Get(IDbProvider provider, int pageSize, int currentPage)
        {
            return ProviderManager.Factory.Tag.Get(provider, pageSize, currentPage);
        }

        public static IList<lb_tag> GetSome(IDbProvider provider, string tags)
        {
            IList<lb_tag> tagList = new List<lb_tag>();
            string[] names = tags.Split(',');
            foreach (string name in names)
            {
                if (name.Length > 0)
                    tagList.Add(ProviderManager.Factory.Tag.Get(provider, Text.SqlEncode(name)));
            }
            return tagList;
        }

        internal static void Add(IDbProvider provider, string name, string intro)
        {
            lb_tag tag = Get(provider, name);
            if (tag == null)
            {
                tag = new lb_tag();
                tag.name = name;
                tag.intro = intro;
                tag.num = 1;
                ProviderManager.Factory.Tag.Insert(provider, tag);
            }
            else
            {
                tag.num++;
                ProviderManager.Factory.Tag.Update(provider, tag);
            }
        }

        internal static void Reduce(IDbProvider provider, string name)
        {
            lb_tag tag = Get(provider, name);
            if (tag != null)
            {
                tag.num--;
                if (tag.num > 0)
                    ProviderManager.Factory.Tag.Update(provider, tag);
                else
                    ProviderManager.Factory.Tag.Delete(provider, tag.id);
            }
        }

        internal static void Edit(IDbProvider provider, string[] oldTags, string[] newTags)
        {
            var addTags = newTags.Where(t =>
            {
                foreach (string oldtag in oldTags)
                {
                    if (oldtag == t)
                        return false;
                }
                return true;
            });
            var reducetags = oldTags.Where(t =>
            {
                foreach (string newtag in newTags)
                {
                    if (newtag == t)
                        return false;
                }
                return true;
            });
            foreach (string addtag in addTags)
                Add(provider, addtag, addtag);
            foreach (string reducetag in reducetags)
                Reduce(provider, reducetag);
        }
    }
}
